/* $Id: NonClientPersonRegisterDelegateImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.delegate.provider;

import java.util.List;

import ae.pcfc.etks.online.common.delegate.NonClientPersonRegisterDelegate;
import ae.pcfc.etks.online.common.facade.AppMetaDataFacade;
import ae.pcfc.etks.online.common.facade.NonClientPersonRegisterFacade;
import ae.pcfc.etks.online.common.vo.ClientCategoryVO;
import ae.pcfc.etks.online.common.vo.NonClientPersonVO;
import ae.pcfc.etks.online.wf.ConstantsWF;
import ae.pcfc.etks.online.wf.service.client.NonClientWF;
import ae.pcfc.etks.online.wf.service.client.provider.NonClientPersonWFImpl;
import ae.pcfc.etks.online.wf.service.client.vo.CompleteStepVO;
import ae.pcfc.etks.online.wf.service.client.vo.NonClientLaunchIncidentVO;

public class NonClientPersonRegisterDelegateImpl extends BaseDelegateImpl
		implements NonClientPersonRegisterDelegate {
	/*
	 * Here we coordinate between the Facade service which handles DB entities
	 * and the WF both will be looked up with service locator normally we will
	 * have 1-1 relationship between the delegate and the facade and 1-1
	 * relationship between the delegate and the WF service which means for
	 * CompanyRegistrationDelegate there will be ClientRegistrationFacade &
	 * CompanyRegistrationWF validations that related to DB vs WF are maintained
	 * here ie user cannot issue a new request while he already have a request
	 * in the wf Exception are translated in to business exception
	 */
	/**
	 * 
	 */
	public NonClientPersonRegisterDelegateImpl() {

	}

	/**
	 * registerNonClientPerson
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.NonClientPersonRegisterDelegate#registerNonClientPerson(ae.pcfc.etks.online.common.vo.NonClientPersonVO)
	 * 
	 */
	public String registerNonClientPerson(NonClientPersonVO nonClientPersonVO) {

		// return
		// serviceLocator.getFacadeService(NonClientPersonRegisterFacade.class).registerNonClientPerson(nonClientPersonVO);
		String clientReferenceNumber = null;
		NonClientPersonRegisterFacade nonClientPersonRegisterFacade = serviceLocator
				.getFacadeService(NonClientPersonRegisterFacade.class);
		AppMetaDataFacade appMetaDataFacade = serviceLocator
				.getFacadeService(AppMetaDataFacade.class);
		NonClientWF nonClientWF = serviceLocator
				.getWorkflowService(NonClientPersonWFImpl.class);
		if (nonClientPersonRegisterFacade
				.validateNonClientPersonReq(nonClientPersonVO)) {
			String applicationID = appMetaDataFacade.getNextApplicationId();
			nonClientPersonVO.setStrApplicationId(applicationID);

			NonClientLaunchIncidentVO launchIncidVO = new NonClientLaunchIncidentVO();
			launchIncidVO.setgApplicationID(applicationID);
			nonClientPersonRegisterFacade.updateWorkflowIncidentVO(
					launchIncidVO, nonClientPersonVO.getArrClientRole());
			Integer wfIncidentId = nonClientWF.launchNonClientWF(launchIncidVO);

			try {
				nonClientPersonVO.setStrWFIncidentId(wfIncidentId.toString());
				clientReferenceNumber = nonClientPersonRegisterFacade
						.registerNonClientPerson(nonClientPersonVO);
			} catch (RuntimeException t) {
				CompleteStepVO completeStepVO = new CompleteStepVO();
				completeStepVO
						.setStrUserName(ConstantsWF.ULTIMUS_DEFAULT_INITIATOR_NAME);
				completeStepVO.setnIncidentNumber(wfIncidentId);
				completeStepVO.setStrSummary(ConstantsWF.ROLLBACK_INCIDENT);
				nonClientWF.abortIncident(completeStepVO);
				throw t;
			}
		}

		return clientReferenceNumber;

	}

	/**
	 * getClientCategory
	 * 
	 * @see ae.pcfc.etks.online.common.delegate.NonClientPersonRegisterDelegate#getClientCategory(java.lang.String)
	 * 
	 */
	public List<ClientCategoryVO> getClientCategory(String clientType) {

		return serviceLocator.getFacadeService(
				NonClientPersonRegisterFacade.class).getClientCategory(
				clientType);
	}

}
